/**
 * @Title:   [DomainUtil.java]
 * @Package: [com.yixun.qfbao.utils]
 * @author:  [shaoping.jiao] 
 * @CreateDate: [2017年11月10日 下午2:40:01]   
 * @UpdateUser: [shaoping.jiao]   
 * @UpdateDate: [2017年11月10日 下午2:40:01]   
 * @Description:  [DomainInterceptor]
 * @version: [V1.0]
 */
package com.yixun.qfbao.interceptor;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.yixun.qfbao.controller.ApiController;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.OperationLogDto;
import com.yixun.qfbao.service.OperationLogService;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.utils.LoginUtils;

/**
 * @ClassName: DomainUtil
 * @author: [shaoping.jiao]
 * @CreateDate: [2017年11月10日 下午2:40:01]
 * @UpdateUser: [shaoping.jiao]
 * @UpdateDate: [2017年11月10日 下午2:40:01]
 * @Description: [DomainInterceptor]
 * @version: [V1.0]
 */
public class LogInterceptor implements HandlerInterceptor {
	
	private static final Logger logger = LoggerFactory.getLogger(ApiController.class);
	
	@Autowired
	private LoginUtils loginUtils;
	@Autowired
	private OperationLogService operationLogService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
		try {
			String userIdStr = request.getHeader("userId");
			if (StringUtils.isEmpty(userIdStr)) {
				userIdStr = request.getParameter("params.userId");
			}
			if(StringUtils.isNotBlank(userIdStr)
					&& !"null".equals(userIdStr)
					&& !"\"\"".equals(userIdStr)
				&& !userIdStr.contains("defined")) {
				PartnerInfo partnerInfo = loginUtils.getUser(Long.parseLong(userIdStr));
				if(null != partnerInfo) {
					String reqUrl = request.getRequestURL().toString();
			    	String target = "";
		    		String indexStr = "";
		    		//查询数据接口不做记录
		    		if(reqUrl.indexOf("/get") > 0) {
		    			return true;
		    		}
		    		if(reqUrl.contains("/api")) {
		    			indexStr = "/api";
		    		}else if(reqUrl.contains("/openapi")) {
		    			indexStr = "/openapi";
		    		}else if(reqUrl.contains("/v10")) {
		    			indexStr = "/v10";
		    		}else if(reqUrl.contains("/agency/weapp/")){
		    			indexStr = "/agency/weapp/";
					}
		    		target = reqUrl.substring(reqUrl.indexOf(indexStr));
		    		
			    	if(StringUtils.isNotBlank(target)) {
			    		OperationLogDto olDto = new OperationLogDto();
			    		if("/api/login".equals(target)){
							olDto.setOptTarget(reqUrl);
						}else{
							olDto.setOptTarget(target);
						}
			    		// 避免post无法获取loginType的问题，直接放在headers中
		    			String loginType = request.getHeader("loginType");
			    		olDto.setOptTargetId(partnerInfo.getHigherid());
			    		olDto.setOptType("99");
			    		olDto.setSystemId(loginType);
			    		olDto.setOptTime(Calendar.getInstance().getTime());
			    		olDto.setOptUser(partnerInfo.getId());
			    		operationLogService.create(olDto);
			    	}
				}
			}
	    }catch (Exception e){
	      logger.error("preHandle error: {}", e);
	    }
	    return true;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}
	
}
